<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form label-position="right" :model="jobDescriptionForm" :label-width="formLabelWidth" ref="refJobDescriptionForm" :rules="rules">
        <el-row>
          <el-col :span="11">
            <el-form-item label="企业名称" prop="enterpriseName">
              <el-input v-model="jobDescriptionForm.enterpriseName" placeholder="请选择企业名称" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findEnterprise"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="岗位名称" prop="jobTitle">
              <el-input v-model="jobDescriptionForm.jobTitle" placeholder="请输入岗位名称" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="部门名称" prop="departmentName">
              <el-input v-model="jobDescriptionForm.departmentName" placeholder="请选择部门名称" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findDepartment"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="工作地点" prop="workLocation">
              <el-input v-model="jobDescriptionForm.workLocation" placeholder="请输入工作地点" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="岗位类型" prop="jobType">
              <el-select v-model="jobDescriptionForm.jobType" placeholder="请选择岗位类型">
                <el-option label="全职" :value="10"></el-option>
                <el-option label="兼职" :value="20"></el-option>
                <el-option label="实习" :value="30"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="薪资范围" prop="salaryRange">
              <el-select v-model="jobDescriptionForm.salaryRange" placeholder="请选择薪资范围">
                <el-option label="3000~5000" :value="1"></el-option>
                <el-option label="5001~10000" :value="2"></el-option>
                <el-option label="10001~20000" :value="3"></el-option>
                <el-option label="20001~30000" :value="4"></el-option>
                <el-option label="30000以上" :value="5"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="发布日期" prop="postingDate">
              <el-date-picker
                  v-model="jobDescriptionForm.postingDate" type="date" placeholder="发布日期" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="截止日期" prop="closingDate">
              <el-date-picker
                  v-model="jobDescriptionForm.closingDate" type="date" placeholder="截止日期" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="招聘人数" prop="headCount">
              <el-input-number v-model="jobDescriptionForm.headCount" :min="1" :max="1"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="状态" prop="status">
              <el-select v-model="jobDescriptionForm.status" placeholder="请选择状态">
                <el-option label="开放" :value="10"></el-option>
                <el-option label="关闭" :value="20"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="岗位职责" prop="responsibilities">
              <el-input type="textarea" :rows="3" v-model="jobDescriptionForm.responsibilities"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="岗位要求" prop="requirement">
              <el-input type="textarea" :rows="3" v-model="jobDescriptionForm.requirement"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="enterpriseSelectorVisible" v-if="enterpriseSelectorVisible" title="企业信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <enterprise-selector @enterprise-selected-event="handleEnterpriseSelectedEvent">
      </enterprise-selector>
    </el-dialog>
    <el-dialog :visible.sync="departmentSelectorVisible" v-if="departmentSelectorVisible" title="部门信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <department-selector @department-selected-event="handleDepartmentSelectedEvent">
      </department-selector>
    </el-dialog>
  </div>
</template>

<script>
import jobDescriptionApi from '@/api/jobDescriptionApi'
import EnterpriseSelector from "@/views/enterprise/EnterpriseSelector.vue";
import DepartmentSelector from "@/views/department/DepartmentSelector.vue";
export default {
  name: 'JobDescriptionAdd',
  components: {EnterpriseSelector,DepartmentSelector,},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      jobDescriptionForm: {
        enterpriseId: '',
        enterpriseName: '',
        jobTitle: '',
        departmentId: '',
        departmentName: '',
        workLocation: '',
        jobType: '',
        responsibilities: '',
        requirement: '',
        salaryRange: '',
        postingDate: '',
        closingDate: '',
        headCount: '',
        status: ''
      },
      rules: {
        enterpriseName: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        jobTitle: [
          { required: true, message: '请输入岗位名称', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        workLocation: [
          { required: true, message: '请输入工作地点', trigger: 'blur' }
        ],
        jobType: [
          { required: true, message: '请输入岗位类型', trigger: 'blur' }
        ],
        responsibilities: [
          { required: true, message: '请输入岗位职责', trigger: 'blur' }
        ],
        requirement: [
          { required: true, message: '请输入岗位要求', trigger: 'blur' }
        ],
        salaryRange: [
          { required: true, message: '请输入薪资范围', trigger: 'blur' }
        ],
        postingDate: [
          { required: true, message: '请输入发布日期', trigger: 'blur' }
        ],
        headCount: [
          { required: true, message: '请输入招聘人数', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请输入状态', trigger: 'blur' }
        ],
      },
      enterpriseSelectorVisible: false,
      departmentSelectorVisible: false,
    }
  },
  created () {
    if (this.id) {
      jobDescriptionApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.jobDescriptionForm = resp.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.jobDescriptionForm = {
        enterpriseId: '',
        enterpriseName: '',
        jobTitle: '',
        departmentId: '',
        departmentName: '',
        workLocation: '',
        jobType: '',
        responsibilities: '',
        requirement: '',
        salaryRange: '',
        postingDate: '',
        closingDate: '',
        headCount: '',
        status: ''
      }
      this.$emit('reset-job-description-add-event')
    },
    onSave () {
      this.$refs.refJobDescriptionForm.validate((valid) => {
        if (valid) {
          jobDescriptionApi.save(this.jobDescriptionForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-job-description-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    findEnterprise () {
      this.enterpriseSelectorVisible = true
    },
    handleEnterpriseSelectedEvent (selectedEnterprise) {
      this.enterpriseSelectorVisible = false
      if (selectedEnterprise && 'enterpriseId' in selectedEnterprise && this.jobDescriptionForm.enterpriseId !== selectedEnterprise['enterpriseId']) {
        this.jobDescriptionForm.enterpriseId = selectedEnterprise['enterpriseId']
        this.jobDescriptionForm.enterpriseName = selectedEnterprise['enterpriseName']
      }
    },
    findDepartment () {
      this.departmentSelectorVisible = true
    },
    handleDepartmentSelectedEvent (selectedDepartment) {
      this.departmentSelectorVisible = false
      if (selectedDepartment && 'departmentId' in selectedDepartment && this.jobDescriptionForm.departmentId !== selectedDepartment['departmentId']) {
        this.jobDescriptionForm.departmentId = selectedDepartment['departmentId']
        this.jobDescriptionForm.departmentName = selectedDepartment['departmentName']
      }
    },
    onBack () {
      this.$emit('close-job-description-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>